package com.jsoft.controller;

import com.alibaba.fastjson.JSON;
import com.jsoft.entity.TicketDetail;
import com.jsoft.service.TicketDetailService;
import com.jsoft.service.impl.TicketDetailServiceImpl;
import com.jsoft.util.ResultUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author:sha
 * @email:o68o4741@163.com
 * @date:2023/2/20
 */
@WebServlet("*.tdc")
public class TransferDetailController extends HttpServlet {
    private static TicketDetailService ticketDetailService = new TicketDetailServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 4);

        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    protected void remainTime(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        /**
         * 前台传回来的到期时间
         */
        String expiryTimeStr = request.getParameter("expiryTime");
        Long expiryTime = Long.parseLong(expiryTimeStr);
        /**
         * 拿到当前系统时间，精确☞年月日
         */
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = dateFormat.format(date);
        /**
         * 将系统当前系统时间转换为时间戳
         */
        long nowTime = 0;
        try {
            Date date1 = dateFormat.parse(nowDate);
            nowTime = date1.getTime();
            /**
             * 做将传过来的时间戳和当前精确到 “天” 的时间戳做差值
             */
            long sub = expiryTime - nowTime;
            long remainDateNumber = sub / 1000 / 60 / 60 / 24;
            out.write(JSON.toJSONString(new ResultUtil("remainDateNumber",remainDateNumber)));
            out.flush();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }

    }
    protected void listTicketDetail(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String idStr = request.getParameter("id");
        Integer id = Integer.valueOf(idStr);
        try {
            TicketDetail ticketDetail = ticketDetailService.searchTicketDetail(id);
            out.write(JSON.toJSONString(new ResultUtil("ticketDetail",ticketDetail)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    protected void listByTransferId(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String enterpriseIdStr = request.getParameter("enterpriseId");
        Integer enterpriseId = Integer.parseInt(enterpriseIdStr);
        try {
            List<TicketDetail> listByTransferId = ticketDetailService.listByTransferId(enterpriseId);
            for (TicketDetail ticketDetail : listByTransferId) {
            }
            List arrTransferIds = new ArrayList();
            for (TicketDetail ticketDetail : listByTransferId) {
                Integer id = ticketDetail.getId();
                arrTransferIds.add(id);
            }
            out.write(JSON.toJSONString(new ResultUtil("arrTransferIds",arrTransferIds)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
